package cn.exrick.xboot.shop.controller;

import cn.exrick.xboot.core.common.utils.PageUtil;
import cn.exrick.xboot.core.common.utils.ResultUtil;
import cn.exrick.xboot.core.common.utils.SecurityUtil;
import cn.exrick.xboot.core.common.vo.PageVo;
import cn.exrick.xboot.core.common.vo.Result;
import cn.exrick.xboot.core.entity.Member;
import cn.exrick.xboot.shop.entity.ShopArticle;
import cn.exrick.xboot.shop.entity.ShopArticleCollect;
import cn.exrick.xboot.shop.entity.ShopComment;
import cn.exrick.xboot.shop.entity.ViewArticle;
import cn.exrick.xboot.shop.service.IShopArticleCollectService;
import cn.exrick.xboot.shop.service.IShopArticleService;
import cn.exrick.xboot.shop.service.IShopCommentService;
import cn.exrick.xboot.shop.service.IViewArticleService;
import cn.exrick.xboot.shop.utils.ArticleUtils;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
* <p>
    * 文章表  前端控制器
    * </p>
*
* @author yuhaifei
* @since 2022-01-21
*/
@RestController
@RequestMapping("/xboot/article")
public class ShopArticleController {

    @Autowired
    private SecurityUtil securityUtil;

    @Autowired
    private IShopArticleService shopArticleService;

    @Autowired
    private IShopCommentService shopCommentService;

    @Autowired
    private IViewArticleService viewArticleService;

    @Autowired
    private IShopArticleCollectService collectService;




    @GetMapping("getArticleList")
    public Result<IPage<ViewArticle>> getArticleList(PageVo pageVo, ViewArticle article){
        Page page = PageUtil.initMpPage(pageVo);
        QueryWrapper<ViewArticle> wrapper = new QueryWrapper<ViewArticle>();
        this.setWrapper(article, wrapper);
        IPage<ViewArticle> ipage = viewArticleService.page(page, wrapper);
        // 获取点赞喜欢情况
        List<ViewArticle> articleList = ipage.getRecords();
        List<Integer> idList = articleList.stream().map(ViewArticle::getId).collect(Collectors.toList());
        String userId = securityUtil.getCurrMember() != null ? securityUtil.getCurrMember().getId():securityUtil.getCurrUser().getId();
        List<ViewArticle> likeList = viewArticleService.findArticleLike(idList, userId);
        ipage.setRecords(likeList);
        return new ResultUtil<IPage<ViewArticle>>().setData(ipage);
    }

    public void setWrapper(ViewArticle article, QueryWrapper<ViewArticle> wrapper){
        if(StrUtil.isNotBlank(article.getValue())){
            wrapper.like("title",article.getValue())
                    .or()
                    .like("content",article.getValue());
        }
        if(StrUtil.isNotBlank(article.getTags()) && !"全部".equals(article.getTags())){
            wrapper.like("tags", article.getTags());
        }
    }

    @PostMapping("addArticle")
    public Result addArticle(ShopArticle article){
        Member m = securityUtil.getCurrMember();
        article.setMemberId(m.getId());
        article.setOpenId(m.getOpenId());
        article.setAvatar(m.getAvatar());
        article.setNickname(m.getNickname());
        article.setMobile(m.getMobile());
        article.setCreateTime(new Date());
        shopArticleService.save(article);
        return ResultUtil.data("添加成功");
    }

    @GetMapping("getArticleById")
    public Result getArticleById(Integer id){
        ViewArticle article = viewArticleService.getById(id);
        // 是否收藏
        ShopArticleCollect collect = collectService.getOne(new QueryWrapper<ShopArticleCollect>()
                .eq("tid", article.getId())
                .eq("user_id", securityUtil.getCurrMember().getId()));
        if(collect != null){
            article.setIsCollect("是");
        }
        return ResultUtil.data(article);
    }

    /**
     * 获取文章详情
     * @param id
     * @return
     */
    @GetMapping("getArticleDetail")
    public Result getArticleDetail(String id){
        // 获取文章详情
        Map<String, Object> detail = new HashMap<>();
        ViewArticle article = viewArticleService.getById(id);
        // 是否收藏
        ShopArticleCollect collect = collectService.getOne(new QueryWrapper<ShopArticleCollect>()
                .eq("tid", article.getId())
                .eq("user_id", securityUtil.getCurrMember().getId()));
        if(collect != null){
            article.setIsCollect("是");
        }
        // 获取评论
        List<ShopComment> list = shopCommentService.selectCommnet(id);
        ArticleUtils.commentNum(list);

        detail.put("article", article);
        detail.put("comment", list);
        return ResultUtil.data(detail);
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @PostMapping("deleteArticle")
    public Result deleteArticle(String id){
        shopArticleService.removeById(id);
        return ResultUtil.data("删除成功");
    }

    /**
     * 获取我的笔记
     * @return
     */
    @GetMapping ("getMyArticle")
    public Result<IPage<ViewArticle>> getMyArticle(PageVo pageVo){
        Page page = PageUtil.initMpPage(pageVo);
        QueryWrapper<ViewArticle> wrapper = new QueryWrapper<ViewArticle>();
        wrapper.eq("member_id", securityUtil.getCurrMember().getId());
        IPage<ViewArticle> ipage = viewArticleService.page(page, wrapper);
        // 获取点赞喜欢情况
        List<ViewArticle> articleList = ipage.getRecords();
        List<Integer> idList = articleList.stream().map(ViewArticle::getId).collect(Collectors.toList());
        String userId = securityUtil.getCurrMember() != null ? securityUtil.getCurrMember().getId():securityUtil.getCurrUser().getId();
        List<ViewArticle> likeList = viewArticleService.findArticleLike(idList, userId);
        ipage.setRecords(likeList);
        return new ResultUtil<IPage<ViewArticle>>().setData(ipage);
    }

    /**
     * 获取我的收藏
     * @return
     */
    @GetMapping ("getMyCollect")
    public Result<IPage<ViewArticle>> getMyCollect(PageVo pageVo){
        Page page = PageUtil.initMpPage(pageVo);
        QueryWrapper<ShopArticleCollect> wrapper = new QueryWrapper<ShopArticleCollect>();
        wrapper.eq("user_id", securityUtil.getCurrMember().getId());
        IPage ipage = collectService.page(page, wrapper);
        // 获取文章列表
        List<ShopArticleCollect> collects = ipage.getRecords();
        List<Integer> idList = collects.stream().map(ShopArticleCollect::getTid).collect(Collectors.toList());

        // 获取点赞喜欢情况
        String userId = securityUtil.getCurrMember() != null ? securityUtil.getCurrMember().getId():securityUtil.getCurrUser().getId();
        List<ViewArticle> likeList = viewArticleService.findArticleLike(idList, userId);

        ipage.setRecords(likeList);
        return new ResultUtil<IPage<ViewArticle>>().setData(ipage);
    }

}
